#include <iostream>
#include <cstdio>
#include <unistd.h>
#include <fcntl.h>


void SetNonBlock(int fd)
{
    int f1 = fcntl(fd, F_GETFL);
    if(f1 < 0)
    {
        perror("fcntl");
        return;
    }

    fcntl(fd, F_SETFL, f1 | O_NONBLOCK);
}

int main()
{
    SetNonBlock(0);
    char buffer[1024];
    while(1)
    {
        ssize_t n = read(0, buffer, sizeof(buffer));
        if(n > 0)
        {
            buffer[n - 1] = 0;
            std::cout << buffer << std::endl;
        }
        else if(n < 0)
        {
            if(errno == EAGAIN || errno == EWOULDBLOCK)
            {
                std::cout << "data not ready..." << std::endl;
                sleep(1);
                continue;
            }
            else if(errno == EINTR)
            {
                continue;
            }
            else
            {
                //读出错

            }
        }
        else
        {
            break;
        }
    }

    // fd_set fset;
    // std::cout << sizeof(fd_set) * 8 << std::endl;
    return 0;
}